package com.zeroxml.cms.web.action.admin;

import com.zeroxml.cms.bo.Article;
import com.zeroxml.cms.bo.Catalog;
import com.zeroxml.cms.service.IArticleService;
import com.zeroxml.cms.service.ICatalogService;
import com.zeroxml.cms.web.action.BaseAdminAction;
import com.wyvoid.core.persistent.hibernate.HibernatePage;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.integration.spring.SpringBean;

import java.util.List;
import java.util.Date;
import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;


/**
 * User: cjp
 * Date: 2007-9-10
 * Time: 22:10:33
 */
@UrlBinding("/admin/articleManage.do")
public class ArticleManageAction extends BaseAdminAction {
    @SpringBean
    private IArticleService articleService;
    @SpringBean
    private ICatalogService catalogService;
    private Article article;
    private HibernatePage<Article> articles;
    private long id;
    private long catalogId;//搜索用
    private String articleName;//搜索用
    private List<Catalog> catalogs;
    private int curPage = 1;

    public void setArticleService(IArticleService articleService) {
        this.articleService = articleService;
    }

    public void setCatalogService(ICatalogService catalogService) {
        this.catalogService = catalogService;
    }

    public HibernatePage<Article> getArticles() {
        return articles;
    }

    public Article getArticle() {
        return article;
    }

    public void setArticle(Article article) {
        this.article = article;
    }

    public void setId(long id) {
        this.id = id;
    }

    public List<Catalog> getCatalogs() {
        return catalogs;
    }


    public long getCatalogId() {
        return catalogId;
    }

    public void setCatalogId(long catalogId) {
        this.catalogId = catalogId;
    }

    public String getArticleName() {
        return articleName;
    }

    public void setArticleName(String articleName) {
        this.articleName = articleName;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    @DefaultHandler
    public Resolution articleList() {
        int pageSize = 2;
        articles = articleService.findArticles(catalogId, articleName, curPage, pageSize);
        return getAdminResolution("articleList");
    }

    public Resolution addArticleForm() {
        if (id > 0) {
            article = articleService.getArticle(id);
        }

        return getAdminResolution("addArticleForm");
    }

    public Resolution addArticle() {
        if (article.getId() > 0) {
            Article tempArticle = articleService.getArticle(article.getId());
            try {
                BeanUtils.copyProperties(tempArticle,article);
            } catch (IllegalAccessException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (InvocationTargetException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
           /* if(tempArticle.getCatalog()!=null&&tempArticle.getCatalog().getId()>0){
                tempArticle.setCatalog(catalogService.getCatalog(tempArticle.getCatalog().getId()));
            }*/
            articleService.updateArticle(tempArticle);
        } else {
            /*if(article.getCatalog()!=null&&article.getCatalog().getId()>0){
                article.setCatalog(catalogService.getCatalog(article.getCatalog().getId()));
            }*/
            article.setCreateTime(new Date());
            articleService.createArticle(article);
        }
        return new RedirectResolution(getClass());
    }

    public Resolution deleteArticle() {
        articleService.deleteArticle(id);
        return new RedirectResolution(getClass());
    }

    @After
    public void init() {
        catalogs = catalogService.getCatalogList(0);
    }
}
